﻿using CPPEI.ETL.DataServiceInterface;
using CPPEI.ETL.Domain;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CPPEI.ETL.Infrastructure;
using ENT = CPPEI.ETL.Domain.Entities;
using MOD = CPPEI.ETL.Domain.Models;

namespace CPPEI.ETL.DSImplement
{
    /// <summary>
    /// 外部程序服务管理者
    /// </summary>
    public class ExtDSDtSerManager : IDtSerManager
    {
        public ServiceResult<Domain.Entities.DtSer> CreateDataService(EtlDbContext dbContext,
            string userId,
            Guid folderId,
            string name,
            string address, 
            bool needAuth, 
            string authUser,
            string authPassword, 
            Guid categoryId, 
            Guid typeId, 
            string description)
        {
            #region
            ENT.DtSerType dtSerType = null;//GetDtSerType(CPPEI.ETL.Domain.Cache.DtSerCategoryList, opParams.TypeId);

            foreach (var category in CPPEI.ETL.Domain.Cache.DtSerCategoryList)
            {
                if (category.Id == categoryId)
                {
                    foreach (var type in category.TypeList)
                    {
                        if (type.Id == typeId)
                        {
                            dtSerType = type;
                            break;
                        }
                    }
                }
            }
            #endregion

            if (dtSerType == null)
            {
                return new ServiceResult<CPPEI.ETL.Domain.Entities.DtSer>(false, ErrorCode.DtSerTypeNotFound, "invalid parameter");
            }
            else
            {
                ExtDtSerProxy proxy = new ExtDtSerProxy();
                if (proxy == null)
                {
                    return new ServiceResult<CPPEI.ETL.Domain.Entities.DtSer>(false, ErrorCode.DtSerProxyNotFound, "invalid parameter");
                }
                else
                {
                    DataServiceInfo serviceInfo = new DataServiceInfo(address);

                    List<TDataSource> dataSources = proxy.EnumerateSources(serviceInfo);

                    DateTime now = DateTime.Now;

                    EntityRepository<ENT.DtSer> serRep = new EntityRepository<ENT.DtSer>(dbContext);
                    EntityRepository<ENT.DtSource> sourceRep = new EntityRepository<ENT.DtSource>(dbContext);
                    EntityRepository<ENT.EDtSourceParameter> pRep = new EntityRepository<ENT.EDtSourceParameter>(dbContext);
                    EntityRepository<ENT.EDtSourceColumn> cRep = new EntityRepository<ENT.EDtSourceColumn>(dbContext);

                    ENT.DtSer dtSer = new ENT.DtSer();
                    dtSer.Id = Guid.NewGuid();
                    dtSer.Name = name;
                    dtSer.Address = address;
                    dtSer.CategoryId = categoryId;
                    dtSer.TypeId = typeId;
                    dtSer.NeedAuth = needAuth;
                    dtSer.AuthUser = authUser;
                    dtSer.AuthPassword = CEncoder.Encode(authPassword);//加密 2017-01-17 
                    dtSer.Creator = userId;
                    dtSer.CreatedTime = now;
                    dtSer.IsDeleted = false;
                    dtSer.IsEnabled = true;
                    dtSer.Description = description;
                    dtSer.FolderId = folderId;
                    serRep.Create(dtSer);

                    foreach (var source in dataSources)
                    {
                        ENT.DtSource dtSource = new ENT.DtSource();
                        dtSource.Id = Guid.NewGuid();
                        dtSource.Name = source.Name;
                        dtSource.Value = source.Value;
                        dtSource.Type = DEF.ExtDSSourceType.Method;
                        dtSource.Creator = userId;
                        dtSource.CreatedTime = now;
                        dtSource.DtSerId = dtSer.Id;
                        sourceRep.Create(dtSource);

                        // insert data source parameters
                        for (int index = 0; index < source.Parameters.Count; index++)
                        {
                            var parameter = source.Parameters[index];
                            ENT.EDtSourceParameter eParameter = new ENT.EDtSourceParameter();
                            eParameter.Id = Guid.NewGuid();
                            eParameter.DataType = parameter.DataType.FullName;
                            eParameter.DtSerId = dtSer.Id;
                            eParameter.DtSourceId = dtSource.Id;
                            eParameter.Name = parameter.Name;
                            eParameter.Index = index;
                            pRep.Create(eParameter);
                        }

                        // insert data source columns
                        for(int index = 0; index <  source.Columns.Count; index++)
                        {
                            TDataSourceColumn oColumn = source.Columns[index];
                            ENT.EDtSourceColumn eColumn = new ENT.EDtSourceColumn();
                            eColumn.Id = Guid.NewGuid();
                            eColumn.DataType = oColumn.DataType.FullName;
                            eColumn.DtSerId = dtSer.Id;
                            eColumn.DtSourceId = dtSource.Id;
                            eColumn.Name = oColumn.Name;
                            eColumn.Index = index;
                            cRep.Create(eColumn);
                        }
                    }
                    return new ServiceResult<CPPEI.ETL.Domain.Entities.DtSer>(true, ErrorCode.COM_NoError, string.Empty, dtSer);
                }
            }
        }

        public ServiceResult CreateDataSource(EtlDbContext dbContext, 
            string userId, 
            Guid dtSerId, 
            string name,
            string value,
            int type, 
            string parameterSettings, 
            string columnSettings, 
            string settings)
        {
            throw new NotImplementedException();
        }

        public bool CanCreateDataSource()
        {
            return false;
        }

        public List<string> EnumerateDataSource(string address, bool needAuth, string authUser, string authPassword)
        {
            throw new NotImplementedException();
        }
    }
}
